<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <script>


        $("#regButton").click(registerUser);

        $("#user_nameReg").blur(userNameJudger);

        $("#user_pwdReg").blur(pwdCompare);

        $("#user_pwdReg2").change(pwdCompare);



        var uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
        var pPattern = /^.*(?=.{6,30})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;

        var nameFlag = false;
        var user_nameReg = $("#user_nameReg").val();
        function userNameJudger(){

            var user_nameReg = $("#user_nameReg").val();
            if(user_nameReg != null && user_nameReg !=''){

                if(uPattern.test(user_nameReg)){

                    console.log("用户名格式正确");

                    $.ajax({
                        url:"<%=path%>/user/queryUserInsByName.do",
                        type:"POST",
                        data:{"user_name":user_nameReg},
                        dataType:"json",
                        success:function(data){
                            if(data.exist==1){
                                $("#nameSp").html("&nbsp;&nbsp;&nbsp; <label class='text-danger'><strong>×用户名已经重复</strong></label>");
                                nameFlag = false;
                                /* alert(data.message); */
                            }else{
                                $("#nameSp").html("&nbsp;&nbsp;&nbsp; <label class='text-success'><strong>√</strong></label>");
                                nameFlag = true;
                                /* alert(data.message); */
                            }
                        }

                    })
                }else{
                    nameFlag = false;
                    console.log("用户名格式不正确");
                    $("#nameSp").html("&nbsp;&nbsp;&nbsp; <label data-toggle='tooltip' data-placement='right'  title='用户名格式错误,应为4到16位.可使用 : 字母，数字，下划线，减号'><p class='text-warning'>💀用户名格式错误,点我查看具体原因.</p></label>");
                    $("[data-toggle='tooltip']").tooltip();

                }
            }else{
                nameFlag = false;
                $("#nameSp").html("&nbsp;&nbsp;&nbsp; <label class='text-danger'><strong>🚫用户名不能为空</strong></label>");
            }
        }

        var pwdFlag = false;


        function pwdCompare(){
            pwdFlag = false;
            var user_pwd1Reg = $("#user_pwdReg").val();
            var user_pwd2 = $("#user_pwdReg2").val();

            if(user_pwd1Reg != null && user_pwd1Reg != ''){
                if(pPattern.test(user_pwd1Reg)){
                    console.log("密码1格式正确");//ok
                    $("#pwd1Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-success'><strong>√</strong></label>");
                    if(user_pwd1Reg===user_pwd2){
                        pwdFlag = true;
                        $("#pwd1Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-success'><strong>√</strong></label>");
                        $("#pwd2Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-success'><strong>√</strong></label>");
                        return pwdFlag;
                    }else{
                        if(user_pwd2==''){
                            pwdFlag = false;
                            $("#pwd2Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-warning'><strong>请输入重复密码.</strong></label>");
                            return pwdFlag;
                        }else{
                            pwdFlag = false;
                            $("#pwd2Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-danger'><strong>重复密码错误.</strong></label>");
                            return pwdFlag;
                        }
                    }
                }else{
                    pwdFlag = false;
                    $("#pwd1Sp").html("&nbsp;&nbsp;&nbsp; <label data-toggle='tooltip' data-placement='right'  title='密码格式错误,需求6-30位,包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符'><p class='text-warning'>💀密码格式错误,点我查看具体原因.</p></label>");
                    $("[data-toggle='tooltip']").tooltip();
                    return pwdFlag;
                }
            }else{
                if(user_pwd2 != null && user_pwd2 != ''){
                    pwdFlag = false;
                    $("#user_pwdReg2").val('');//ok
                    console.log("密码2被清空,先填写密码1");//ok
                    $("#user_pwdReg").focus();//ok
                    $("#pwd2Sp").html("&nbsp;&nbsp;&nbsp; <label class='text-warning'><strong>请先输入上面的密码.</strong></label>");
                    return pwdFlag;
                }

            }

        }
        function registerUser(){
            console.log("in registerUser() ~");
            var user_nameTrue = $("#user_nameReg").val();
            var user_pwdTrue = $("#user_pwdReg").val();
            userNameJudger();
            if(pwdCompare() && nameFlag){

                $.ajax({
                    url:"<%=path%>/user/insertNewUser.do",
                    type:"POST",
                    data:{"user_name":user_nameTrue,"user_pwd":user_pwdTrue},
                    dataType:"json",
                    success:function(data){
                        if(data.insertNum>0){
                            console.log("注册成功");
                            console.log("用户名:"+user_nameTrue+".密码:"+user_pwdTrue);
                            document.forms["regForm"].submit();
                            window.location.href="http://localhost:8084/OrderSystemM/Test1/userinfo.jsp";
                            //
                        }else{
                            console.log("注册失败");
                        }
                    }
                })
            }else{
                console.log("🚫阻止提交");
                event.preventDefault();
            }

        }


    </script>
